package com.dpj.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/*
  Compared to timer,the api's advantage is that it could many threads,
  and even though there is only  one thread in the pool,even through
  the previous thread has problem, the next work wouldn't be affected
 */
@Slf4j(topic = "c.TestScheduledTheadPool")
public class TestScheduledTheadPool {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.schedule(()->{
            log.debug("task1");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },1, TimeUnit.SECONDS);

        pool.schedule(()->{
            log.debug("task2");
        },1, TimeUnit.SECONDS);
    }
}
